package com.axzhengxin.platform.assistant.service.impl;

import com.axzhengxin.platform.assistant.domain.BizMarketInfo;
import com.axzhengxin.platform.assistant.domain.BizMarketUser;
import com.axzhengxin.platform.assistant.mapper.BizMarketInfoMapper;
import com.axzhengxin.platform.assistant.mapper.BizMarketUserMapper;
import com.axzhengxin.platform.assistant.service.IBizMarketInfoService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author axzhengxin
 * @date 2021-03-05
 */
@Service
public class BizMarketInfoServiceImpl implements IBizMarketInfoService
{
    @Autowired
    private BizMarketInfoMapper bizMarketInfoMapper;
    @Autowired
    private BizMarketUserMapper bizMarketUserMapper;
    /**
     * 查询【请填写功能名称】
     * 
     * @param marketId 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public BizMarketInfo selectBizMarketInfoById(Long marketId)
    {
        return bizMarketInfoMapper.selectBizMarketInfoById(marketId);
    }

    @Override
    public BizMarketInfo selectBizMarketInfoLittleById(Long marketId) {
        return bizMarketInfoMapper.selectBizMarketInfoLittleById(marketId);
    }

    @Override
    @Transactional
    public String importBizMarkrtInfo(List<BizMarketInfo> bizMarketInfoList) {
        if (StringUtils.isNull(bizMarketInfoList) || bizMarketInfoList.size() == 0) {
            throw new CustomException("导入营销名单数据不能为空！");
        }
        AtomicInteger successNum = new AtomicInteger();
        AtomicInteger failureNum = new AtomicInteger();
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        Long userid= SecurityUtils.getLoginUser().getUser().getUserId();
        Long deptId= SecurityUtils.getLoginUser().getUser().getDeptId();
        bizMarketInfoList.parallelStream().forEach(bizMarketInfo ->{
            BizMarketInfo bizMarketInfo1 = bizMarketInfoMapper.selectBizMarketInfoByName(bizMarketInfo.getCorName());
            if (StringUtils.isNull(bizMarketInfo1)){
                int i = bizMarketInfoMapper.insertBizMarketInfo(bizMarketInfo);
                if ( i > 0 ){
                    BizMarketUser bizMarketUser = new BizMarketUser();
                    bizMarketUser.setMarketId(bizMarketInfo.getMarketId());
                    bizMarketUser.setMarketUser(userid);
                    bizMarketUser.setMarketUserDept(deptId);
                    bizMarketUser.setMarketStatus("0");
                    bizMarketUserMapper.insertBizMarketUser(bizMarketUser);
                    successNum.getAndIncrement();
                    successMsg.append("<br/>").append(successNum.get()).append("、公司 ").append(bizMarketInfo.getCorName()).append(" 导入成功");
                }else {
                    failureNum.getAndIncrement();
                    failureMsg.append("<br/>").append(failureNum.get()).append("、公司 ").append(bizMarketInfo.getCorName()).append(" 导入失败");
                }
            }else {
                failureNum.getAndIncrement();
                failureMsg.append("<br/>").append(failureNum.get()).append("、公司 ").append(bizMarketInfo.getCorName()).append(" 已存在");
            }
        });
        if (failureNum.get() > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param bizMarketInfo 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<BizMarketInfo> selectBizMarketInfoList(BizMarketInfo bizMarketInfo)
    {
        return bizMarketInfoMapper.selectBizMarketInfoList(bizMarketInfo);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param bizMarketInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertBizMarketInfo(BizMarketInfo bizMarketInfo)
    {
        return bizMarketInfoMapper.insertBizMarketInfo(bizMarketInfo);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param bizMarketInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateBizMarketInfo(BizMarketInfo bizMarketInfo)
    {
        return bizMarketInfoMapper.updateBizMarketInfo(bizMarketInfo);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param marketIds 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteBizMarketInfoByIds(Long[] marketIds)
    {
        return bizMarketInfoMapper.deleteBizMarketInfoByIds(marketIds);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param marketId 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteBizMarketInfoById(Long marketId)
    {
        return bizMarketInfoMapper.deleteBizMarketInfoById(marketId);
    }
}
